package com.gemantic.auth.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.gemantic.auth.service.AuthSessionService;
import com.google.common.collect.Maps;

@Controller
@CrossOrigin
public class LoginController {
	
	Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private AuthSessionService authSessionService;
	
	@GetMapping("/index")
	public String firstCheck(HttpServletRequest request) {
		String originalUrl = request.getParameter("originalUrl");
		String ssoUser = request.getParameter("ssoUser");
		String token = null;
		boolean loginFlag = false;
		if(ssoUser!=null && ssoUser.trim().length()>0) {
			//对用户先判断是否已经登陆过
			token = authSessionService.getUserToken(ssoUser);
			if(token!=null) {
				loginFlag = true;
			}
		}
		if(loginFlag) {
			//判断如果用户已经在SSO-Server认证过，直接发送token
			boolean resu;
			try {
				resu = authSessionService.tokenTrans(request,originalUrl,ssoUser,token);
				if(resu) {
					if(originalUrl!=null) {
						if(originalUrl.contains("?")) {
							originalUrl = originalUrl + "&ssoUser="+ssoUser;
						}else {
							originalUrl = originalUrl + "?ssoUser="+ssoUser;
						}
					}
				}
				return "redirect:"+originalUrl;
			} 
			catch (Exception e) {
				logger.error(e.getMessage(), e);
				return "redirect:/loginPage?originalUrl="+request.getParameter("originalUrl");
			}
		}else {
			//需要替换成专业点的路径,自己登陆下了
			return "redirect:/loginPage?originalUrl="+request.getParameter("originalUrl");
		}
	}
	
	//登陆界面，返回的是页面地址
	@GetMapping("/loginPage")
	public String index(HttpServletRequest request) {
		if(request.getParameter("originalUrl")!=null) {
			request.setAttribute("originalUrl", request.getParameter("originalUrl"));
		}
		return "login";
	}
	
	//登陆逻辑,返回的是令牌
	@PostMapping(value="/doLogin")
	public String login(HttpServletRequest request, HttpServletResponse response,
			String userName, String password, String originalUrl) {
		
		if(authSessionService.verify(userName,password)) {
			String token = authSessionService.cacheSession(userName);
			boolean resu;
			try {
				resu = authSessionService.tokenTrans(request,originalUrl,userName,token);
				if(resu) {
					//跳转到提示成功的页面
					request.setAttribute("helloName", userName);
					logger.info("login sucess....." + userName);
					if(originalUrl!=null) {
						if(originalUrl.contains("?")) {
							originalUrl = originalUrl + "&ssoUser="+userName;
						}else {
							originalUrl = originalUrl + "?ssoUser="+userName;
						}
						request.setAttribute("originalUrl", originalUrl);
					}
					logger.info("originalUrl  " + originalUrl);
					return "redirect:"+originalUrl;
				}
				//return "hello";//TO-DO 三秒跳转
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		//验证不通过，重新来吧
		if(originalUrl!=null) {
			request.setAttribute("originalUrl", originalUrl);
		}
		return "login";
	}
	
}